package com.sk.waste.app.adapters

import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.drawable.Drawable
import android.transition.Transition
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.Nullable
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.target.Target
import com.lc.smc.lib.interfaces.LCInterface
import com.lc.smc.lib.utils.LCBaseUtil
import com.sk.waste.app.R
import com.sk.waste.app.application.MyApplication
import com.sk.waste.app.config.NetWorkConfig
import com.sk.waste.app.models.SpaceModel
import com.sk.waste.app.models.UserModel
import com.sk.waste.app.utils.BaseUtil


/**
 *  @time 2025/01/07  15:37
 *  @desc 空间
 */
class SpaceAdapter(val context: Activity, val type: Int, val list: List<SpaceModel>, val callback: LCInterface.LcCallBack?) :
    RecyclerView.Adapter<SpaceAdapter.ViewHolder>() {
    var userInfo: UserModel? = null

    init {
        userInfo = MyApplication.instance.getUserInfo()
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SpaceAdapter.ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.adapter_space, parent, false))
    }

    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, @SuppressLint("RecyclerView") position: Int) {

        val item = list[position]
        holder.ttvName.text = item.name
        if (isVip(item.isVip)) {
            holder.imgVip.visibility = View.VISIBLE
        } else {
            holder.imgVip.visibility = View.GONE
        }

        holder.ttvTime.text = item.publishTime
        holder.ttvLocation.text = item.publishLocation
        holder.ttvContent.text = item.infoContent
        holder.ttvLook.text = "浏览：" + item.viewCount

        Glide.with(holder.imgAvatar.context)
            .load(NetWorkConfig.NET_URL_IMA_ROOT + item.avatarUrl)
            .transform(CenterCrop(), RoundedCorners(20))
            .placeholder(R.mipmap.img_user_default)
            .error(R.mipmap.img_user_default)
            .into(holder.imgAvatar);

        // 如果正在快速滑动，只加载占位图
        if (item.imgList.size != 0) {
            Glide.with(holder.imgShow.context)
                .load(NetWorkConfig.NET_URL_IMA_ROOT + item.imgList[0])
                .transform(CenterCrop(), RoundedCorners(20))
                .error(R.mipmap.img_banner_default)
                .into(holder.imgShow);
        } else {
            holder.imgShow.setImageResource(R.mipmap.img_banner_default)
            holder.imgShow.scaleType = ImageView.ScaleType.FIT_XY
        }

        holder.ttvCall.tag = position
        holder.ttvCall.setOnClickListener {
//            if (userInfo?.isVip() == true) {
            val cp = holder.ttvCall.tag as Int
            BaseUtil.callPhone(context, list[cp].userPhone)
//            }
        }

        holder.rootView.tag = position
        holder.rootView.setOnClickListener {
            val currentSelect = holder.rootView.tag as Int
            callback!!.callBack(currentSelect)
        }
    }

    fun mNotifyDataSetChanged() {
        userInfo = MyApplication.instance.getUserInfo()
        notifyDataSetChanged()
    }

    fun isVip(vip: String): Boolean {
        return !LCBaseUtil.isNull(vip) && "VIP".equals(vip, true)
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val imgAvatar: ImageView = view.findViewById(R.id.item_space_icon)
        val ttvName: TextView = view.findViewById(R.id.item_space_name)
        val imgVip: ImageView = view.findViewById(R.id.item_space_vip)
        val ttvTime: TextView = view.findViewById(R.id.item_space_time)
        val ttvLocation: TextView = view.findViewById(R.id.item_space_location)
        val ttvContent: TextView = view.findViewById(R.id.item_space_content)
        val imgShow: ImageView = view.findViewById(R.id.item_space_image)
        val ttvLook: TextView = view.findViewById(R.id.item_space_look)
        val ttvCall: TextView = view.findViewById(R.id.item_space_call)
        val rootView: ConstraintLayout = view.findViewById(R.id.view_top_layout)
    }


}